/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.controller.bean;

import com.entites.Employee;
import com.model.bean.EmployeeFacadeLocal;
import java.io.IOException;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Chung
 */
@ManagedBean(name = "login")
@SessionScoped
public final class Login implements Serializable {

    private final String SLASH = "/";
    private final String USERNAME = "username";
    private final String PASSWORD = "passwd";
    private final String HOMEPAGE = "/Home.xhtml";
    private final String LOGINSESSION = "login";
    private final String ERRORMESSAGE = "UserName or password you entered is incorrect";

    private String username;
    private String password;
    private String fullname;
    private String error;
    private String avatar;

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }
    private boolean rememberUser = false;

    @EJB
    private EmployeeFacadeLocal employeeFacadeLocal;

    public Login() {
    }

    public boolean isRememberUser() {
        return rememberUser;
    }

    public void setRememberUser(boolean rememberUser) {
        this.rememberUser = rememberUser;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void action() throws IOException {
        checkAcc();
    }

    private void redirect(String url) throws IOException {
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        context.redirect(context.getRequestContextPath() + url);
    }

    private void checkAcc() throws IOException {
        Employee em = employeeFacadeLocal.find(username);
        if (em != null) {
            if (em.getPassword().equals(password)) {
                fullname = em.getFullname();
                avatar = em.getAvatar();
                saveLoginToSession();
                checkRememberPawd();
                redirect(HOMEPAGE);
            }
        }
        error = ERRORMESSAGE;
    }

    private void checkRememberPawd() {
        if (rememberUser) {
            RememberMe();
        }
    }

    private void RememberMe() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Cookie btuser = new Cookie(USERNAME, username);
        Cookie btpasswd = new Cookie(PASSWORD, password);
        btuser.setMaxAge(50600);
        btpasswd.setMaxAge(50600);
        btpasswd.setPath(SLASH);
        btuser.setPath(SLASH);
        ((HttpServletResponse) facesContext.getExternalContext().getResponse()).addCookie(btuser);
        ((HttpServletResponse) facesContext.getExternalContext().getResponse()).addCookie(btpasswd);

    }

    private void saveLoginToSession() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);
        Login user = new Login();
        user.setFullname(fullname);
        user.setUsername(username);
        user.setAvatar(avatar);
        session.setAttribute(USERNAME, username);
        session.setMaxInactiveInterval(50600);
        session.setAttribute(LOGINSESSION, user);

    }

    public String logout() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        HttpSession session = (HttpSession) request.getSession(true);
        session.setAttribute(LOGINSESSION, null);
        removeCookie();

        return "Login?faces-redirect=true";

    }

    private void removeCookie() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Cookie btuser = new Cookie(USERNAME, username);
        Cookie btpasswd = new Cookie(PASSWORD, password);
        btuser.setMaxAge(-1);
        btpasswd.setMaxAge(-1);
        btpasswd.setPath(SLASH);
        btuser.setPath(SLASH);
        ((HttpServletResponse) facesContext.getExternalContext().getResponse()).addCookie(btuser);
        ((HttpServletResponse) facesContext.getExternalContext().getResponse()).addCookie(btpasswd);
    }
public String getUsernameSession() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);
        String usn = (String) session.getAttribute(USERNAME);
        return usn;
    }
}
